package com.yanqu.road.server.cmd.activity;


import com.yanqu.road.entity.activity.ActivityDisperseGoldInfo;
import com.yanqu.road.logic.pb.ActivityDisperseGoldPb;
import com.yanqu.road.pb.activity.ActivityDisperseGoldProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityDisperseGoldMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.C_CROSS_ACTIVITY_DISPERSE_GOLD_RESULT_PUSH, desc = "活动送财结果")
public class ActivityDisperseGoldResultCmd extends CrossCommand {

    ActivityDisperseGoldInfo tempGoldInfo;

    ActivityDisperseGoldProto.ActivityDisperseGoldInfoTemp goldInfoTemp;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        goldInfoTemp = ActivityDisperseGoldProto.ActivityDisperseGoldInfoTemp.parseFrom(packet.getBytes());
        tempGoldInfo = ActivityDisperseGoldPb.parseToDisperseGoldInfo(goldInfoTemp);
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ActivityDisperseGoldInfo goldInfo = ActivityDisperseGoldMgr.getActivityDisperseGoldInfo(tempGoldInfo.getDisperseId());
        if(goldInfo == null || goldInfo.isSendEmail()){
            return;
        }

        if(goldInfoTemp.getCanMail()){
            //回传总数据
            ActivityDisperseGoldProto.ActivityDisperseGoldInfoTemp.Builder rebackGoldTemp = ActivityDisperseGoldPb.parseToDisperseGoldInfoTemp(goldInfo);
            MessageHelper.sendPacket(goldInfo.getServerId(), goldInfo.getUserId(), YanQuMessageUtils.buildMessage(Protocol.S_ACTIVITY_DISPERSE_GOLD_MAIL_NOTICE, rebackGoldTemp));
            goldInfo.setSendEmail(true);
        }else {
            //汇总数据
            synchronized (goldInfo){
                goldInfo.setPlayerNum(goldInfo.getPlayerNum() + tempGoldInfo.getPlayerNum());
                Property reward = PropertyHelper.parseStringToProperty(goldInfo.getReward());
                if(reward == null){
                    reward = new Property();
                }
                Property crosssReward = PropertyHelper.parseStringToProperty(tempGoldInfo.getReward());
                reward.addProperty(crosssReward);
                goldInfo.setReward(PropertyHelper.parsePropertyToString(reward));
            }
        }
    }

    @Override
    public void sendData() {

    }
}
